package com.qianbao.qianbaobusiness.ui.cashier.databill;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianbao.qianbaobusiness.R;


/**
 * Created by hdb on 2017/2/17.
 */

public abstract class BaseToolBarActivity extends BaseCommActivity {
    public Toolbar toolbar;
    private ToolBarHelper mToolBarHelper;
    ImageView iv;
    private TextView title;
//    private TextView mTvLeft;
    private TextView subtitle;
    private ImageView navigation;
    private TextView right_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar();
    }

    @Override
    public void setContentView(int layoutResID) {
        mToolBarHelper = new ToolBarHelper(this, layoutResID);
        toolbar = mToolBarHelper.getToolBar();
        title = (TextView) toolbar.findViewById(R.id.title);
//        mTvLeft = (TextView) toolbar.findViewById(R.id.tv_left_tb);

        subtitle = (TextView) toolbar.findViewById(R.id.subtitle);
        setContentView(mToolBarHelper.getContentView());
        toolbar.setBackgroundColor(getResources().getColor(R.color.app_white));

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            toolbar.setPadding(0, Tools.dip2px(getThis(), 25), 0, 0);
//
//        }else {
//            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) toolbar.getLayoutParams();
//            params.height = Tools.dip2px(getThis(), 48);
//            toolbar.setLayoutParams(params);
//            toolbar.setMinimumHeight(Tools.dip2px(getThis(), 48));
//        }
//        toolbar.setNavigationIcon(R.drawable.icon_back_white);
        /*把 toolbar 设置到Activity 中*/
//        setSupportActionBar(toolbar);
//        setDisplayHomeAsUpEnabled(true);
//        setDisplayShowCustomEnabled(true);
//        setDisplayShowHomeEnabled(true);
//        setDisplayShowTitleEnabled(false);
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                OnNavigationClick(v);
//            }
//        });

        /*自定义的一些操作*/
        onCreateCustomToolBar(toolbar);
        iv = (ImageView) toolbar.findViewById(R.id.right_swip_icon);
        right_text = (TextView) toolbar.findViewById(R.id.right_text);
        navigation = (ImageView) toolbar.findViewById(R.id.navigation_icon);

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rightClick();
            }
        });
        right_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rightClick();
            }
        });
        if (isClickable()) {
            title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    titleClick();
                }
            });
            subtitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    subtitleClick();
                }
            });
        }
        navigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnNavigationClick(v);
            }
        });
//        mTvLeft.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                OnNavigationClick(v);
//            }
//        });
    }

    protected abstract boolean isClickable();

    protected abstract void subtitleClick();

    protected abstract void titleClick();

    protected abstract void rightClick();

    protected abstract void initToolBar();

    public void onCreateCustomToolBar(Toolbar toolbar) {
        toolbar.setContentInsetsRelative(0, 0);
    }

    protected void setDisplayHomeAsUpEnabled(boolean b) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(b);
    }

    protected void setDisplayShowHomeEnabled(boolean b) {
        getSupportActionBar().setDisplayShowHomeEnabled(b);
    }

    protected void setDisplayShowTitleEnabled(boolean b) {
        getSupportActionBar().setDisplayShowTitleEnabled(b);
    }

    protected void setDisplayShowCustomEnabled(boolean b) {
        getSupportActionBar().setDisplayShowCustomEnabled(b);
    }

    protected void setToolBarBackgroundColor(int resId) {
//        if(Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT
//                || Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT_WATCH) {
//            View view = getWindow().getDecorView().findViewById(R.id.TRANSLUCENT_VIEW);
//            view.setBackgroundColor(getResources().getColor(resId));
//        } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            getWindow().setStatusBarColor(getResources().getColor(resId));
//        }
        toolbar.setBackgroundColor(getResources().getColor(resId));
    }

    protected void setToolBarBackground(int d) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            toolbar.setBackground(getResources().getDrawable(d));
        }
    }

    protected void setNavigationIcon(int resId) {
        navigation.setImageResource(resId);
    }
    protected  void hideNagationIcon(){
        navigation.setVisibility(View.GONE);
    }
    protected void setNavigationIcon(Drawable icon) {
        navigation.setImageDrawable(icon);
    }

    protected void OnNavigationClick(View v) {
        finish();
    }

    public Toolbar getToolbar() {
        return toolbar;
    }

    protected void hideToolbar() {
        toolbar.setVisibility(View.GONE);
    }

    protected void showToolbar() {
        toolbar.setVisibility(View.VISIBLE);
    }

    protected void setToolBarTitle(int resourceId) {
        setToolBarTitle(getString(resourceId));
    }

    protected void setToolBarSubtitle(int resourceId) {
        setToolBarSubtitle(getString(resourceId));
    }

    /**
     * 设置标题
     *
     * @param text
     */
    protected void setToolBarTitle(CharSequence text) {
        if (text == null) {
            return;
        }

        if (title != null) {
            title.setText(text);
            if (title.getVisibility() == View.GONE) {
                title.setVisibility(View.VISIBLE);
            }
        }
    }

//    protected  void showLeftTv(){
//        mTvLeft.setVisibility(View.VISIBLE);
//    }
//
//    protected void setLeftTvColor(int color){
//        if (mTvLeft != null) {
//            mTvLeft.setTextColor(getResources().getColor(color));
//        }
//    }
//
//    protected void setLeftTv(String left){
//        if (mTvLeft != null) {
//            mTvLeft.setText(left);
//        }
//    }

    /**
     * 设置标题字体色
     *
     * @param color
     */
    protected void setToolBarTitleColor(int color) {

        if (title != null) {
            title.setTextColor(getResources().getColor(color));
        }
    }

    /**
     * 设置标题背景
     *
     */
    protected void setToolBarTitleBackground(@DrawableRes int drawable) {
        if (title != null) {
            title.setBackgroundResource(drawable);
        }
    }

    /**
     * 设置副标题
     *
     * @param text
     */
    protected void setToolBarSubtitle(CharSequence text) {
        if (text == null) {
            return;
        }
        if (subtitle.getVisibility() == View.GONE) {
            subtitle.setVisibility(View.VISIBLE);
        }
        if (subtitle != null) {
            subtitle.setText(text);
            if (subtitle.getVisibility() == View.GONE) {
                subtitle.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 设置副标题字体颜色
     *
     * @param color
     */
    protected void setToolBarSubtitleColor(int color) {

        if (subtitle != null) {
            if (subtitle.getVisibility() == View.GONE) {
                subtitle.setVisibility(View.VISIBLE);
            }
            subtitle.setTextColor(getResources().getColor(color));
        }
    }

    /**
     * 设置副标题背景
     *
     */
    protected void setToolBarSubtitleBackground(@DrawableRes int drawable) {
        if (subtitle != null) {
            if (subtitle.getVisibility() == View.GONE) {
                subtitle.setVisibility(View.VISIBLE);
            }
            subtitle.setBackgroundResource(drawable);
        }
    }

    protected void setRightIcon(int resId, boolean show) {
        if (!show) {
            iv.setVisibility(View.GONE);
            return;
        }
        if (iv != null) {
            iv.setVisibility(View.VISIBLE);
            iv.setImageResource(resId);
        }
    }

    protected void setRightIcon(Drawable d, boolean show) {
        if (!show) {
            iv.setVisibility(View.GONE);
            return;
        }
        if (iv != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                iv.setVisibility(View.VISIBLE);
                iv.setBackground(d);
            }
        }
    }

    protected void showRightIcon() {
        iv.setVisibility(View.VISIBLE);
    }

    protected void setRightIcon(int imgId) {
        Drawable drawable = this.getResources().getDrawable(imgId);
        iv.setImageDrawable(drawable);
    }

    protected void hideRightIcon() {
        iv.setVisibility(View.GONE);
    }
    protected void showRightText() {
        right_text.setVisibility(View.VISIBLE);
    }

    protected void hideRightText() {
        right_text.setVisibility(View.GONE);
    }

    protected void setTitleClickable(boolean isClickable) {
        title.setEnabled(isClickable);
        title.setClickable(isClickable);

    }

    protected void setSubtitleClickable(boolean isClickable) {
        subtitle.setEnabled(isClickable);
        subtitle.setClickable(isClickable);
    }
    protected void setRight_text(String str){
        right_text.setText(str);
    }
    protected void setRight_textColor(int color){
        right_text.setTextColor(getResources().getColor(color));
    }
}
